{
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "version": "3.6.5-final"
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3,
  "kernelspec": {
   "name": "python36564bitbasecondaa7de243d99944edfaea41320dfc97b65",
   "display_name": "Python 3.6.5 64-bit ('base': conda)"
  }
 },
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import cv2\n",
    "\n",
    "label_list = [\"one\",\"two\",\"three\",\"four\",\"five\",\"good\"]\n",
    "model_dir = r\"E:\\pycharm(2018)\\Graduated_project\\vgg_model\"\n",
    "model_name = 'ckp_10000'\n",
    "model_path = os.path.join(model_dir,model_name)\n",
    "\n",
    "def recognize(random_data):\n",
    "    tf.reset_default_graph()\n",
    "    random_data = random_data.reshape((1,30,30,3))\n",
    "    with tf.Session() as sess:\n",
    "\n",
    "        saver = tf.train.import_meta_graph(model_path+'.meta')\n",
    "        graph = tf.get_default_graph()\n",
    "        x = graph.get_tensor_by_name(\"x:0\")\n",
    "        predict = graph.get_tensor_by_name(\"predict:0\")\n",
    "        saver.restore(sess,tf.train.latest_checkpoint(model_dir))\n",
    "        result = sess.run(predict,feed_dict={x:random_data})\n",
    "        print(\"the image is %s\"%(label_list[result[0]-1]))\n",
    "        final_result = label_list[result[0] - 1]\n",
    "    return final_result\n",
    "\n",
    "def preprocess_data(data):\n",
    "    img = cv2.resize(data,(30,30),interpolation=cv2.INTER_AREA)\n",
    "    img = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)\n",
    "    #这里记住归一化！，因为在测试集和训练集的时候都是已经归一化了的！！！！\n",
    "    img = img / 127.5 - 1 \n",
    "    return img"
   ]
  },
  {
   "cell_type": "markdown",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# R键识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cv2\n",
    "import os\n",
    "# link = \"E:\\pycharm(2018)\\Graduated_project\\data\"\n",
    "# link_one = os.path.join(link,\"one\")\n",
    "#肤色定义\n",
    "lower_skin = np.array([0,50,0])\n",
    "upper_skin = np.array([60,255,255])\n",
    "#窗口定义\n",
    "ptLeftTop = (100, 100)\n",
    "ptRightBottom = (400, 400)\n",
    "point_color = (0, 255, 0) # BGR\n",
    "thickness = 2 \n",
    "lineType = 4\n",
    "\n",
    "cap = cv2.VideoCapture(0)\n",
    "\n",
    "num = 0\n",
    "\n",
    "def img_process(frame):\n",
    "    img_hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)\n",
    "    \n",
    "    #滤出肤色区域\n",
    "    mask = cv2.inRange(img_hsv, lower_skin, upper_skin)\n",
    "    mask_morph = mask.copy()\n",
    "    #选择kernel\n",
    "    kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))\n",
    "    #腐蚀 和 膨胀 操作\n",
    "    mask_morph = cv2.morphologyEx(mask_morph,cv2.MORPH_OPEN,kernel)\n",
    "    mask_morph = cv2.morphologyEx(mask_morph,cv2.MORPH_CLOSE,kernel)\n",
    "    \n",
    "    output = cv2.bitwise_and(frame ,frame, mask=mask_morph)\n",
    "    return output,mask_morph\n",
    "\n",
    "while True:\n",
    "    ret,frame = cap.read()\n",
    "    frame = cv2.resize(frame,(960,720))\n",
    "\n",
    "    output, mask_morph = img_process(frame)\n",
    "    cv2.rectangle(frame, ptLeftTop, ptRightBottom, point_color, thickness, lineType)\n",
    "    #capture_windows\n",
    "    cap_windows = mask_morph[ptLeftTop[1]:ptRightBottom[1], ptLeftTop[0]:ptRightBottom[0]]\n",
    "    cap_windows_real = output[ptLeftTop[1]:ptRightBottom[1], ptLeftTop[0]:ptRightBottom[0]]\n",
    "    default_title = \"Press Q: quit, Press R: recognize\"\n",
    "    cv2.putText(frame, default_title, (30, 30), cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 255), 2)\n",
    "    cv2.imshow(\"original\",frame)\n",
    "    cv2.imshow(\"cap_windows\",cap_windows)\n",
    "\n",
    "\n",
    "    if cv2.waitKey(1) & 0XFF is ord(\"r\"):\n",
    "        data = preprocess_data(cap_windows)\n",
    "        result = recognize(data)\n",
    "        text1 = \"Recognized_result:\" \n",
    "        text2 = str(result)\n",
    "        cv2.putText(cap_windows_real, text1, (30, 30), cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 255), 2)\n",
    "        cv2.putText(cap_windows_real, text2, (30, 50), cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 255), 2)\n",
    "        cv2.imshow(\"cap_windows_real\",cap_windows_real)\n",
    "\n",
    "    if cv2.waitKey(1) & 0xFF is ord('q'):\n",
    "        break\n",
    "cap.release()\n",
    "cv2.destroyAllWindows()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实时识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cv2\n",
    "import os\n",
    "# link = \"E:\\pycharm(2018)\\Graduated_project\\data\"\n",
    "# link_one = os.path.join(link,\"one\")\n",
    "#肤色定义\n",
    "lower_skin = np.array([0,50,0])\n",
    "upper_skin = np.array([60,255,255])\n",
    "#窗口定义\n",
    "ptLeftTop = (100, 100)\n",
    "ptRightBottom = (400, 400)\n",
    "point_color = (0, 255, 0) # BGR\n",
    "thickness = 2 \n",
    "lineType = 4\n",
    "\n",
    "cap = cv2.VideoCapture(0)\n",
    "\n",
    "num = 0\n",
    "\n",
    "def img_process(frame):\n",
    "    img_hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)\n",
    "    \n",
    "    #滤出肤色区域\n",
    "    mask = cv2.inRange(img_hsv, lower_skin, upper_skin)\n",
    "    mask_morph = mask.copy()\n",
    "    #选择kernel\n",
    "    kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))\n",
    "    #腐蚀 和 膨胀 操作\n",
    "    mask_morph = cv2.morphologyEx(mask_morph,cv2.MORPH_OPEN,kernel)\n",
    "    mask_morph = cv2.morphologyEx(mask_morph,cv2.MORPH_CLOSE,kernel)\n",
    "    \n",
    "    output = cv2.bitwise_and(frame ,frame, mask=mask_morph)\n",
    "    return output,mask_morph\n",
    "\n",
    "while True:\n",
    "    ret,frame = cap.read()\n",
    "    frame = cv2.resize(frame,(960,720))\n",
    "\n",
    "    output, mask_morph = img_process(frame)\n",
    "    cv2.rectangle(frame, ptLeftTop, ptRightBottom, point_color, thickness, lineType)\n",
    "    #capture_windows\n",
    "    cap_windows = mask_morph[ptLeftTop[1]:ptRightBottom[1], ptLeftTop[0]:ptRightBottom[0]]\n",
    "    cap_windows_real = output[ptLeftTop[1]:ptRightBottom[1], ptLeftTop[0]:ptRightBottom[0]]\n",
    "    default_title = \"Press Q: quit, real_time recognize\"\n",
    "    cv2.putText(frame, default_title, (30, 30), cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 255), 2)\n",
    "    cv2.imshow(\"original\",frame)\n",
    "    cv2.imshow(\"cap_windows\",cap_windows)\n",
    "\n",
    "\n",
    "    # if cv2.waitKey(1) & 0XFF is ord(\"r\"):\n",
    "    data = preprocess_data(cap_windows)\n",
    "    result = recognize(data)\n",
    "    text1 = \"Recognized_result:\" \n",
    "    text2 = str(result)\n",
    "    cv2.putText(cap_windows_real, text1, (0, 30), cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 255), 2)\n",
    "    cv2.putText(cap_windows_real, text2, (30, 60), cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 255), 2)\n",
    "    cv2.imshow(\"cap_windows_real\",cap_windows_real)\n",
    "\n",
    "    if cv2.waitKey(1) & 0xFF is ord('q'):\n",
    "        break\n",
    "cap.release()\n",
    "cv2.destroyAllWindows()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}